# Encoding: utf-8
#
# Authors:: api.dklimkin@gmail.com (Danial Klimkin)
#
# Copyright:: Copyright 2012, Google Inc. All Rights Reserved.
#
# License:: Licensed under the Apache License, Version 2.0 (the "License");
#           you may not use this file except in compliance with the License.
#           You may obtain a copy of the License at
#
#           http://www.apache.org/licenses/LICENSE-2.0
#
#           Unless required by applicable law or agreed to in writing, software
#           distributed under the License is distributed on an "AS IS" BASIS,
#           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
#           implied.
#           See the License for the specific language governing permissions and
#           limitations under the License.
#
# AdWords API Rakefile.

lib = File.expand_path('../lib/', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

# Google common ads library used for wrapper code generation.
require 'ads_common/build/savon_generator'

require 'adwords_api/api_config'

desc 'Default target - generate and build.'
task :default => [:generate, :build]

desc 'Package the AdWords API library into a gem file.'
task :build do
  result = system('/usr/bin/env gem build google-adwords-api.gemspec')
  raise 'Build failed.' unless result
end

desc 'Perform the unit testing.'
task :test do
  result = system('/usr/bin/env ruby test/suite_unittests.rb')
  raise 'Unit tests failed.' unless result
end

desc 'Perform the examples testing.'
task :test_examples do
  Dir.glob('test/suite_exampletests_v*.rb').each do |file|
    puts 'Running test suite: %s' % file
    result = system('/usr/bin/env ruby %s' % file)
    raise 'Example tests failed for %s.' % file unless result
  end
end

desc 'Perform integration testing.'
task :test_integrate do
  Dir.glob('test/suite_integration.rb').each do |file|
    puts 'Running test suite: %s' % file
    result = system('/usr/bin/env ruby %s' % file)
    raise 'Integration tests failed for %s.' % file unless result
  end
end

desc 'Perform smoke testing.'
task :test_smoke => [:test, :test_integrate]

desc 'Run tests coverage tool.'
task :coverage do
  result = system('/usr/bin/env ruby test/coverage.rb')
  raise 'Coverage run failed.' unless result
end

desc 'Package demo apps.'
task :build_demo do
  ['adwords_on_rails'].each do |app|
    result = system('tar cz --mode +r,u+X+w -C examples -f %s.tar.gz %s' %
        [app, app])
    raise 'Demo packaging failed for %s.' % app unless result
  end
end

desc 'Generate the Adwords API stubs.'
task :generate do
  logger = Logger.new(STDOUT)
  logger.level = Logger::INFO
  api_config = AdwordsApi::ApiConfig
  versions = api_config.versions()
  versions.each do |version|
    code_path = 'lib/%s/%s' % [api_config.api_name.to_s.snakecase, version]
    wsdls = AdwordsApi::ApiConfig.get_wsdls(version)
    wsdls.each do |service_name, wsdl_url|
      logger.info('Processing %s at [%s]...' % [service_name, wsdl_url])
      generator = AdsCommon::Build::SavonGenerator.new(
          wsdl_url, code_path, api_config.api_name, version, service_name)
      generator.process_wsdl()
    end
  end
end
